package cn.szsys.integration.util;

import cn.hutool.core.text.CharSequenceUtil;
import cn.szsys.integration.config.filter.auth.ReqSignParam;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * @author Ryan
 * @since 2025-05-08
 **/
@Slf4j
public class SignUtil {
    /**
     * @param ak accessKey
     * @param as accessKeySecret
     * @return 加密的字符串（ak+time用as使用AES加密算法加密）
     */
    public static Sign aesEncode(String ak, String as) {
        long time = System.currentTimeMillis();
        return new Sign(ak, EncryptUtil.ECB.encode(ak + time, as), time);
    }

    /**
     * @param encStr AES加密后的字符串
     * @param as     accessKeySecret
     * @return 解密后的字符串
     */
    public static Sign aesDecode(String encStr, String as) {
        Sign sign = new Sign();
        sign.setStr(EncryptUtil.ECB.decode(encStr, as));
        return sign;
    }

    /**
     * @param ak     accessKey
     * @param encStr AES加密后的字符串
     * @param time   时间戳
     * @param as     accessKeySecret
     */
    public static boolean validateSign(String ak, String encStr, long time, String as) {
        //return (ak + time).equals(aesDecode(encStr, as).getStr());
        return validateSignAes(ak, encStr, time, as, null);
    }

    public static boolean validateSignAes(String ak, String encStr, long time, String as, Integer ver) {
        if (ver != null) {
            if (ver == 1) {
                char[] chs = (ak + time).toCharArray();
                Arrays.sort(chs);
                String src = new String(chs);
                return src.equals(aesDecode(encStr, as).getStr());
            }
            return false;
        } else {
            return (ak + time).equals(aesDecode(encStr, as).getStr());
        }
    }

    /**
     * @param signParam signParam
     * @return {@link boolean}
     * @author Ryan
     */
    public static boolean validateSign(ReqSignParam signParam) {
        return validateSignAes(signParam.getAk(), signParam.getSign(), signParam.getTime(), signParam.getAks(), signParam.getVer());
    }

    /**
     * @param ak      ak
     * @param encStr  sign
     * @param time    时间戳
     * @param as      AccessKeySecret
     * @param alg     算法
     * @param version 版本
     * @return true-验签通过，false-验签失败
     */
    public static boolean validateSign(String ak, String encStr, long time, String as, String alg, Integer version) {
        if (CharSequenceUtil.isBlank(alg) || "1".equals(alg)) {
            return validateSignAes(ak, encStr, time, as, version);
        } else {
            if ("2".equals(alg)) {
                //DES
                log.debug("DES-alg:{}", alg);
            } else {
                log.debug("alg:{}", alg);
            }
            return false;
        }
    }

    public static boolean validateSignAes(String origin, String encode, String as) {
        return origin.equals(aesDecode(encode, as).getStr());
    }

    public static Sign sign(String ak, String aks) {
        long time = System.currentTimeMillis();
        char[] chs = (ak + time).toCharArray();
        Arrays.sort(chs);
        String src = new String(chs);
        return new Sign(ak, EncryptUtil.ECB.encode(src, aks), time);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Sign {
        private String ak;
        private String str;
        private Long time;
    }
}
